﻿using UnityEngine;
using System.Collections;

public class SummonerSpell : Card {

	public bool forAllies;
	public bool spellMenuOpen;
	public bool forNexus;
	// Use this for initialization
	void Start () {
	
	}
	
	public void initializeSummonerSpellStuff(){
		myType = type.summonerspell;
	}

	public virtual float evaluateSummonerSpell(Champion champion){
		return 0.0f;
	}

	public virtual float evaluateSummonerSpell(Nexus nexus){
		return 0.0f;
	}

	public virtual void playSpell(Champion champion){
		spellMenuOpen = false;
		menuOpen = false;
		closeMenu();
		if(GameController.gc.playerOneTurn){
			GameController.gc.playerOneHand.afterPlay(handPositon);
		}
		else{
			GameController.gc.playerTwoHand.afterPlay(handPositon);
		}
		removeBackground();
		transform.position = new Vector3(champion.transform.position.x, champion.transform.position.y, champion.transform.position.z-5);
		StartCoroutine(waitToPutInGraveyard());
		inHand = false;
		GameController.gc.updateLastAction(cardName+" played on "+champion.cardName);
		return;
	}
	public virtual void playSpell(Nexus nexus){
		spellMenuOpen = false;
		menuOpen = false;
		closeMenu();
		if(GameController.gc.playerOneTurn){
			GameController.gc.playerOneHand.afterPlay(handPositon);
		}
		else{
			GameController.gc.playerTwoHand.afterPlay(handPositon);
		}
		removeBackground();
		transform.position = new Vector3(nexus.transform.position.x, nexus.transform.position.y, nexus.transform.position.z-5);;
		StartCoroutine(waitToPutInGraveyard());
		inHand = false;
		GameController.gc.updateLastAction(cardName+" played on Nexus");
		return;
	}

	void openSpellMenu(){
		spellMenuOpen=true;
	}

	void OnGUI(){
		if(!GameController.gc.gameover){
			if(spellMenuOpen){
				if(!forNexus){
					for(int i = 0; i < 4; i++){
						for(int j = 0; j < 4; j++){
							GridCell targetGridCell = GameController.gc.board[i,j];
							if(targetGridCell.hasCard){
								if((targetGridCell.card.player1Card && forAllies) || (!targetGridCell.card.player1Card && !forAllies)){
									Vector3 cardPosition = Camera.main.WorldToScreenPoint(targetGridCell.card.transform.position);
									if (GUI.Button(new Rect(cardPosition.x-GameController.gc.cardSize.x/2, Screen.height - GameController.gc.cardSize.y/2 - cardPosition.y, GameController.gc.cardSize.x, GameController.gc.cardSize.y), "")){
										Champion target = targetGridCell.card;
										playSpell(target);
									}
								}
							}
						}
					}
				}
				else{
					Vector3 position = Camera.main.WorldToScreenPoint(GameController.gc.playerOneNexus.transform.position);
					if(GUI.Button(new Rect(position.x-GameController.gc.cardSize.x/2,Screen.height - position.y-GameController.gc.cardSize.y/2, GameController.gc.buttonSize.x/3, GameController.gc.buttonSize.y), "")){
						Nexus target = GameController.gc.playerOneNexus;
						playSpell (target);
					}
				}
				if(GUI.Button(new Rect(0,Screen.height-50, 100, 50), "Cancel")){
					spellMenuOpen = false;
				}
			}
			else if (viewMenuOpen){
				GUI.Box(new Rect(Screen.width * 1/5,Screen.height * 1/10,Screen.width * 3/5,Screen.height * 3/5), GameController.gc.blackBackgroud);
				GUI.DrawTexture(new Rect(Screen.width * 2/10 + 10,Screen.height * 2/10,Screen.width * 2/5,Screen.height * 2/5), cardSprite);
				GUI.Label(new Rect(Screen.width * 5/10,Screen.height * 2/10,Screen.width * 2/10,Screen.height * 1/20),"Summoner Spell Name: "+ cardName);
				GUI.Label(new Rect(Screen.width * 5/10,Screen.height * 4/10,Screen.width * 2/10,Screen.height * 5/20),"Summoner Spell Description: "+ cardDescription);
				if(GUI.Button(new Rect(Screen.width * 2/5, Screen.height * 3/5, Screen.width * 1/10, Screen.height * 1/10), "Close"))
					viewMenuOpen = false;
			}
			else if(menuOpen){
				if(GUI.Button(new Rect(Screen.width/2 + 75, Screen.height/2, GameController.gc.buttonSize.x, GameController.gc.buttonSize.y), "Play"))
					openSpellMenu();
				if(GUI.Button(new Rect(Screen.width/2 - 225, Screen.height/2, GameController.gc.buttonSize.x, GameController.gc.buttonSize.y), "View"))
					view ();
				if(GUI.Button(new Rect(Screen.width/2 - 75, Screen.height/2 + 50, GameController.gc.buttonSize.x, GameController.gc.buttonSize.y), "Close"))
					closeMenu();
			}
		}
	}
}
